﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Interfejsi;
using MySql.Data.MySqlClient;
using sistem;

namespace DAL
{
    partial class DAL
    {
        public class CelijaDAO : IDaoCrud<Celija>
        {
            #region IDaoCrud<Celija> Members
            protected MySqlCommand c;
            public long create(Celija entity)
            {
                try
                {
                    c = new MySqlCommand("insert into celija (dan_u_sedmici, pocetak, predmet_id, profesor_id, razred_id, kabinet_id) values (@DanUSedmici, @Pocetak, @PredmetID, @ProfesorID, @RazredID, @KabinetID);", con);
                    c.Parameters.AddWithValue("@DanUSedmici", entity.DanUSedmici);
                    c.Parameters.AddWithValue("@Pocetak", entity.Pocetak);
                    c.Parameters.AddWithValue("@PredmetID", entity.PredmetID);
                    c.Parameters.AddWithValue("@ProfesorID", entity.ProfesorID);
                    c.Parameters.AddWithValue("@RazredID", entity.RazredID);
                    c.Parameters.AddWithValue("@KabinetID", entity.KabinetID);
                    return c.LastInsertedId;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Celija> getAll()
            {
                try
                {
                    c = new MySqlCommand("select * from celija;", con);
                    MySqlDataReader r = c.ExecuteReader();
                    List<Celija> celijaovi = new List<Celija>();
                    while (r.Read())
                    {
                        Celija celija = new Celija();
                        celija.ID = r.GetInt32("id");
                        celija.DanUSedmici = r.GetString("dan_u_sedmici");
                        celija.Pocetak = r.GetString("pocetak");
                        celija.PredmetID = r.GetInt32("predmet_id");
                        celija.ProfesorID = r.GetInt32("profesor_id");
                        celija.RazredID = r.GetInt32("razred_id");
                        celija.KabinetID = r.GetInt32("kabinet_id");
                        celijaovi.Add(celija);
                    }
                    return celijaovi;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Celija read(Celija entity)
            {
                try
                {
                    c = new MySqlCommand("select * from celija where id = @ID;", con);
                    c.Parameters.AddWithValue("@ID", entity.ID);
                    MySqlDataReader r = c.ExecuteReader();
                    Celija celija = new Celija();
                    if (r.Read())
                    {
                        celija.ID = r.GetInt32("id");
                        celija.DanUSedmici = r.GetString("dan_u_sedmici");
                        celija.Pocetak = r.GetString("pocetak");
                        celija.PredmetID = r.GetInt32("predmet_id");
                        celija.ProfesorID = r.GetInt32("profesor_id");
                        celija.RazredID = r.GetInt32("razred_id");
                        celija.KabinetID = r.GetInt32("kabinet_id");
                    }
                    return celija;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Celija update(Celija entity)
            {
                try
                {
                    c = new MySqlCommand("UPDATE celija SET dan_u_sedmici = @DanUSedmici, pocetak = @Pocetak, predmet_id = @PredmetID, profesor_id = @ProfesorID, razred_id = @RazredID, kabinet_id = @KabinetID where id = @ID;", con);
                    c.Parameters.AddWithValue("@DanUSedmici", entity.DanUSedmici);
                    c.Parameters.AddWithValue("@Pocetak", entity.Pocetak);
                    c.Parameters.AddWithValue("@PredmetID", entity.PredmetID);
                    c.Parameters.AddWithValue("@ProfesorID", entity.ProfesorID);
                    c.Parameters.AddWithValue("@RazredID", entity.RazredID);
                    c.Parameters.AddWithValue("@KabinetID", entity.KabinetID);
                    c.Parameters.AddWithValue("@ID", entity.ID);
                    c.ExecuteNonQuery();
                    return entity;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public void delete(Celija entity)
            {
                try
                {
                    c = new MySqlCommand("delete from celija where id = @ID;", con);
                    c.Parameters.AddWithValue("@ID", entity.ID);
                    c.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Celija getById(int id)
            {
                try
                {
                    c = new MySqlCommand("select * from celija where id = @ID;", con);
                    c.Parameters.AddWithValue("@ID", id);
                    MySqlDataReader r = c.ExecuteReader();
                    Celija celija = new Celija();
                    if (r.Read())
                    {
                        celija.ID = r.GetInt32("id");
                        celija.DanUSedmici = r.GetString("dan_u_sedmici");
                        celija.Pocetak = r.GetString("pocetak");
                        celija.PredmetID = r.GetInt32("predmet_id");
                        celija.ProfesorID = r.GetInt32("profesor_id");
                        celija.RazredID = r.GetInt32("razred_id");
                        celija.KabinetID = r.GetInt32("kabinet_id");
                    }
                    return celija;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Celija> getByExample(string name, string value)
            {
                try
                {
                    c = new MySqlCommand("select * from celija where @Kolona = @Vrijednost;", con);
                    c.Parameters.AddWithValue("@Kolona", name);
                    c.Parameters.AddWithValue("@Vrijednost", value);
                    MySqlDataReader r = c.ExecuteReader();
                    List<Celija> celijaovi = new List<Celija>();
                    while (r.Read())
                    {
                        Celija celija = new Celija();
                        celija.ID = r.GetInt32("id");
                        celija.DanUSedmici = r.GetString("dan_u_sedmici");
                        celija.Pocetak = r.GetString("pocetak");
                        celija.PredmetID = r.GetInt32("predmet_id");
                        celija.ProfesorID = r.GetInt32("profesor_id");
                        celija.RazredID = r.GetInt32("razred_id");
                        celija.KabinetID = r.GetInt32("kabinet_id");
                        celijaovi.Add(celija);
                    }
                    return celijaovi;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Celija> getForUcenik(int value)
            {
                try
                {
                    c = new MySqlCommand("select * from celija where razred_id = @Vrijednost;", con);
                    c.Parameters.AddWithValue("@Vrijednost", value);
                    MySqlDataReader r = c.ExecuteReader();
                    List<Celija> celije = new List<Celija>();
                    while (r.Read())
                    {
                        Celija celija = new Celija();
                        celija.ID = r.GetInt32("id");
                        celija.DanUSedmici = r.GetString("dan_u_sedmici");
                        celija.Pocetak = r.GetString("pocetak");
                        celija.Predmet = r.GetString("predmet");
                        celija.ProfesorID = r.GetInt32("profesor_id");
                        celija.RazredID = r.GetInt32("razred_id");
                        celija.Kabinet = r.GetString("kabinet");
                        celije.Add(celija);
                    }
                    return celije;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            #endregion
        }
    }
}